/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrn.adurn.architecture.dao;

import br.ufrn.adurn.domain.Centro;
import java.sql.PreparedStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;

/**
 *
 * @author Victor
 */
public class CentroDAO {

    private Connection connection;

    public CentroDAO() {
        this.connection = new ConnectionFactory().getConnection();
    }

    public void adiciona(Centro centro) {
        String sql = "INSERT INTO centros (nome, sigla, email, tel1, tel2) values (?,?,?,?,?)";
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            stmt.setString(1, centro.getNome());
            stmt.setString(2, centro.getSigla());
            stmt.setString(3, centro.getEmail());
            stmt.setString(4, centro.getTel1());
            stmt.setString(5, centro.getTel2());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        // DEVE SER CHAMADO NAS CLASSES DE INTERFACE GRAFICA
        JOptionPane.showMessageDialog(null, "Centro " + centro.getNome() + " adicionado com sucesso", "", JOptionPane.INFORMATION_MESSAGE);
    }

    public Long getIdBySigla(String sigla) {
        Long idCentro = null;
        try {
            PreparedStatement stmt = this.connection.prepareStatement("select id_centro from centros where sigla like '" + sigla + "'");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                idCentro = rs.getLong("id_centro");
            }
            rs.close();
            stmt.close();
            return idCentro;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public String getSiglaById(Long id) {
        String sigla = null;
        try {
            PreparedStatement stmt = this.connection.prepareStatement("select sigla from centros where id_centro like '" + id + "'");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                sigla = rs.getString("sigla");
            }
            rs.close();
            stmt.close();
            return sigla;
            //return novoCentro;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public List<Centro> getLista() {
        try {
            List<Centro> centros = new ArrayList<Centro>();
            PreparedStatement stmt = this.connection.prepareStatement("select * from centros");
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                Centro centro = new Centro();
                centro.setId(rs.getLong("id_centro"));
                centro.setNome(rs.getString("nome"));
                centro.setSigla(rs.getString("sigla"));
                centro.setEmail(rs.getString("email"));
                centro.setTel1(rs.getString("tel1"));
                centro.setTel2(rs.getString("tel2"));
                centros.add(centro);
            }
            rs.close();
            stmt.close();
            return centros;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public void remove(Centro centro) {
        try {
            PreparedStatement stmt = connection.prepareStatement("delete from centros where id_centro=?");
            stmt.setLong(1, centro.getId());
            stmt.execute();
            stmt.close();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
